/**
 * @desc
 *
 * @使用场景
 *
 * @author  杨晓东[of1081]
 * @company qianmi.com
 * @Date    16/11/17
 **/
'use strict';
Object.defineProperty(exports, "__esModule", { value: true });
/**
 * 根据模板配置生成相应的的模板JS与CSS
 * @param templateIndexArray
 * @return {string}
 */
async function generateTplJSCss(templateCompileParams) {
    let templateJs = '', templateCss = '';
    if (templateCompileParams.templateConfig && templateCompileParams.templateConfig.dependenciesBar && templateCompileParams.templateConfig.dependenciesBar.length > 0) {
        for (let i = 0, ilen = templateCompileParams.templateConfig.dependenciesBar.length; i < ilen; i++) {
            let widgetNs = templateCompileParams.templateConfig.dependenciesBar[i];
            let barDefaultPropsMap;
            if (templateCompileParams.templateConfig.barDefaultPropsMap) {
                barDefaultPropsMap = templateCompileParams.templateConfig.barDefaultPropsMap[widgetNs];
            }
            let { js, css, pJs, pCss } = await _loadWidgetRelFile({ widgetNs, loadWidgetFile: templateCompileParams.loadWidgetFile });
            let widgetDefaultProps = barDefaultPropsMap;
            if (widgetDefaultProps) {
                if (typeof widgetDefaultProps === 'function') {
                    //把函数源代码赋值过去.
                    widgetDefaultProps = widgetDefaultProps.toString();
                }
                else {
                    widgetDefaultProps = JSON.stringify(widgetDefaultProps);
                }
                let _begIndex = pJs.indexOf('__XSite_widgetInject');
                let _startIndex = pJs.indexOf('{', _begIndex) + 1;
                let _preContent = pJs.substring(0, _startIndex);
                let _postContent = pJs.substring(_startIndex);
                pJs = _preContent + `widgetDefaultProps:${widgetDefaultProps},` + _postContent;
            }
            templateJs += js;
            templateJs += pJs;
            templateCss += css;
            templateCss += pCss;
        }
        templateJs += `;(function(){
            window.__XSite_template_register(${JSON.stringify(templateCompileParams.templateConfig)});
            xDllContext.iflux.msg.emit("__XSite_Template_Loaded_${templateCompileParams.templateConfig.templateName}", "${templateCompileParams.templateConfig.templateName}");
        })();`;
    }
    return { templateJs, templateCss };
}
exports.generateTplJSCss = generateTplJSCss;
async function _loadWidgetRelFile({ widgetNs, loadWidgetFile }) {
    let widgetPath;
    let widgetJs, widgetPJs, widgetCss = "", widgetPCss = "";
    // console.log('读取每个组件的js -p.js .css',distDir,widgetNs,'========================================================');
    widgetJs = await loadWidgetFile({ widgetFilePath: widgetNs + ".js" });
    widgetPJs = await loadWidgetFile({ widgetFilePath: widgetNs + "-p.js" });
    try {
        widgetCss = await loadWidgetFile({ widgetFilePath: widgetNs + ".css" });
    }
    catch (err) {
        console.warn(widgetNs + ".css 加载失败, 可能本组件就不需要这个样式文件");
    }
    try {
        widgetPCss = await loadWidgetFile({ widgetFilePath: widgetNs + "-p.css" });
    }
    catch (err) {
        console.warn(widgetNs + "-p.css 加载失败, 可能本组件就不需要这个样式文件");
    }
    return { js: widgetJs, css: widgetCss, pJs: widgetPJs, pCss: widgetPCss };
}
